package com.yonyou.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yonyou.dao.entity.BOVO;
import com.yonyou.dao.entity.TemplateModelVO;
import com.yonyou.dao.entity.TemplateVO;
import com.yonyou.print.params.Constant;
import com.yonyou.print.params.ParamsEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by yijx on 2018/1/5.
 */
@Service("cloudPrintService")
public class CloudPrintService {
    @Autowired
    private HttpService httpService;
    /**
     * 从云端查询所有BO
     * @return
     */
    public List<BOVO> getCloudBOs(){
        // 查询该租户下所有BO
        Map<String, String> param = new HashMap<String, String>();
        Object BOInfos = httpService.httpGetRequest(Constant.queryAllBOUrl, param);
        //json转VO
        if(BOInfos == "结果空"){
            return new ArrayList<BOVO>();
        }
        List<BOVO>  BOs= JSONArray.parseObject(BOInfos.toString(),new TypeReference<ArrayList<BOVO>>(){});
        return BOs;
    }

    /**
     * 查询云端该租户下所有模板
     * @return
     */
    public List<TemplateVO> getCloudTemplates(){
        Map<String, String> param1 = new HashMap<String, String>();
        param1.put("tetantId", ParamsEngine.getInfoConfig().getProperty("tenantId"));
        Object templateInfos = httpService.httpPostRequest(Constant.queryAllTempUrl, param1);
        //json转VO
        List<TemplateVO> VOs = JSONArray.parseObject(templateInfos.toString(),new TypeReference<ArrayList<TemplateVO>>(){});
        return VOs;
    };

    /**
     * 从云端查询打印模板模型
     * @param bo_template
     * @return
     */
    public List<TemplateModelVO> getPrintModel(JSONArray bo_template) {
        List<TemplateModelVO> templateModelVOS = new ArrayList<TemplateModelVO>();
        for(int i=0;i<bo_template.size();i++) {
            JSONObject template = bo_template.getJSONObject(i);
            Map<String, String> printParam = new HashMap<String, String>();
            String bo_code = template.getString("bo_code");
            printParam.put("bo_code",bo_code);
            printParam.put("code",  template.getString("templateCode"));

            JSONObject dynamicsArr = new JSONObject();
            dynamicsArr.put("name", bo_code);
            dynamicsArr.put("datarow", "1");
            printParam.put("dynamicsArr", "[" + dynamicsArr.toString() + "]");

            Object model = httpService.httpPostRequest(Constant.queryPrintModelUrl, printParam);
            TemplateModelVO templateModelVO = JSON.parseObject(model.toString(),new TypeReference<TemplateModelVO>(){});
            for(int j=0;j<templateModelVO.getModels().size();j++){
                templateModelVO.setPk_print_template(template.getString("pk_print_template"));
                templateModelVO.getModels().get(j).setPk_print_template(template.getString("pk_print_template"));
            }
            templateModelVOS.add(templateModelVO);
        }
        return templateModelVOS;
    }

    /**
     * 从云端删除模板
     * @param param
     * @return
     */
    public String deleteCloudTemplate(Map<String,String> param){
        Object  result = httpService.httpPostRequest(Constant.deleteTempUrl, param);
        return result.toString();
    }

    public String copyCloudTemplate(Map<String,String> param){
        Object result = httpService.httpPostRequest(Constant.copyTempUrl, param);
        return result.toString();
    }
}
